{extend name="mainadmin@layouts/base" /}
{block name="main" }

<header class="header  b-b clearfix">
     <div class="page-breadcrumbs">
            <ul class="breadcrumb">
                <li>
                    <i class="fa fa-ellipsis-v"></i>
                    <strong>{$row['id']>0?'编辑':'添加'}奖项</strong>
                </li>                                  
            </ul>
           <a class="text-muted pull-right pointer p-r m-t-md" data-toggle="back" title="返回"><i class="fa fa-reply"></i></a>
        </div>
</header>
<section class="scrollable  wrapper">
 <form class="form-horizontal form-validate" method="post" action="{:url('info')}" style="padding:0;">
        <section class="panel panel-default">
                <div class="panel-body">            			
                		<div class="form-group">
                            <label class="control-label">奖项名称：</label>
                            <div class="col-sm-6">
                                <input type="text" class="input-large" data-rule-maxlength="20" data-rule-required="true" name="reward_name" value="{$row.reward_name}" ><span class="maroon">*</span>
                        </div>
                 </div>

                    <div class="form-group">
                        <label class="control-label">奖项类型：</label>
                        <select name="reward_type" id="">
                            <option value="">请选择</option>
                            <option value="3" {if condition="$row.reward_type == 3"} selected {/if}>省代理商</option>
                            <option value="2" {if condition="$row.reward_type == 2"} selected {/if}>市代理商</option>
                            <option value="1" {if condition="$row.reward_type == 1"} selected {/if}>区代理商</option>
                        </select>
                    </div>

                    <div class="form-group">
                        <label class="control-label">区域代理奖：</label>
                        <div class="col-sm-6">
                            <input type="text" class="input-large" data-rule-maxlength="20" data-rule-required="true" name="region_reward" value="{$row.region_reward}" ><span class="maroon">%*</span>
                        </div>
                    </div>



                <div class="form-group">
                    <div class="form-group">
                        <label class="control-label">极差奖：</label>
                        <div class="col-sm-6">
                            <input type="text" class="input-large" data-rule-maxlength="20" data-rule-required="true" name="range_reward" value="{$row.range_reward}" ><span class="maroon">%*</span>
                        </div>
                    </div>

<!--                    <div class="form-group">-->
<!--                        <div class="form-group">-->
<!--                            <label class="control-label">奖项名称：</label>-->
<!--                            <div class="col-sm-6">-->
<!--                                <input type="text" class="input-large" data-rule-maxlength="20" data-rule-required="true" name="reward_name" value="{$row.reward_name}" ><span class="maroon">*</span>-->
<!--                            </div>-->
<!--                        </div>-->


            <div class="line line-dashed line-lg pull-in"></div>
            <div class="form-group">
                <div class="col-sm-4 col-sm-offset-2">
                    <button type="submit" class="btn btn-primary" data-loading-text="保存中...">保存</button>
                    <button type="button" class="btn btn-default" data-toggle="back">取消</button>
                </div>
            </div>
        </div>
    </section>
     <input name="id" type="hidden" value="{$row.id|intval}">
    </form>
</section>

{include file="shop@/sys_admin/goods/sel_goods" /}
<!--{literal}-->
<!--<script type="text/html" id="d_level_tr">-->
<!--<tr class="d_level_tr">-->
<!--	<td align="center">{{val}}<input name="award_value[{{key}}][level]" type="hidden" value="{{key}}" /></td>-->
<!--	<td ><input type="text" name="award_value[{{key}}][name]" class="input-max" data-rule-required="true" value="{{name}}"></td>-->
<!--	<td style="text-align: right;">-->
<!--            <input type="text" name="award_value[{{key}}][num]" class="input-ssmall" min="0.01" data-rule-ismoney="true"  value="{{num}}">-->
<!--            <select name="award_value[{{key}}][num_type]"><option value="money" {{num_type=='money'?'selected':''}}>固定金额</option><option value="per" {{num_type=='per'?'selected':''}}>%百分比</option></select>-->
<!--    </td>-->
<!--    <td align="center"><a href="javascript:;" title="删除" onClick="delLevel({{key}});"><i class="fa fa-trash-o text-muted"></i></a></td>-->
<!--</tr>-->
<!--</script>-->
<!--<script type="text/html" id="d_role_goods_tr">-->
<!--    <tr class="d_role_goods_tr">-->
<!--        <td align="center">{{val}}<input name="award_value[{{key}}][level]" type="hidden" value="{{key}}" /></td>-->
<!--        <td ><input type="text" name="award_value[{{key}}][name]" class="input-max" data-rule-required="true" value="{{name}}"></td>-->
<!--        <td style="text-align: right;">-->
<!--            {{each roleList as role index}}-->
<!--            <p>-->
<!--                {{role.role_name}}：-->
<!--                <input type="text" name="award_value[{{key}}][num][{{role.role_id}}]" class="input-ssmall" min="0" data-rule-ismoney="true"  value="{{num[role.role_id]}}">-->
<!--                <select name="award_value[{{key}}][num_type][{{role.role_id}}]"><option value="money" {{num_type[role.role_id]=='money'?'selected':''}}>固定金额</option><option value="per" {{num_type[role.role_id]=='per'?'selected':''}}>%百分比</option></select>-->
<!--            </p>-->
<!--            {{/each}}-->
<!--        </td>-->
<!--        <td align="center"><a href="javascript:;" title="删除" onClick="delLevel({{key}});"><i class="fa fa-trash-o text-muted"></i></a></td>-->
<!--    </tr>-->
<!--</script>-->
<!--<script type="text/html" id="d_role_tr">-->
<!--<tr >-->
<!--	<td align="center">{{role_name}}<input name="role_award_value[{{role_id}}][role_id]" type="hidden" value="{{role_id}}" /></td>-->
<!--	<td ><input type="text" name="role_award_value[{{role_id}}][name]" class="input-max" data-rule-required="true" value="{{name}}"></td>-->
<!--	<td ><input type="text" name="role_award_value[{{role_id}}][num]" class="input-ssmall" min="0.01" data-rule-ismoney="true"   value="{{num}}"><select name="role_award_value[{{role_id}}][num_type]"><option value="money" {{num_type=='money'?'selected':''}}>固定金额</option><option value="per" {{num_type=='per'?'selected':''}}>%百分比</option></select></td>-->
<!--</tr>-->
<!--</script>-->
<!--{/literal}-->
{/block}

{block name="footer"}
<script type="text/javascript">
var d_level = {$d_level|json_encode|raw};

var award_value = {$row['award_value']|raw};
var award_type = {$row['award_type']|intval};
var data = [];
data.select_type = 'buy_goods';
data.goodsList = {$row['buy_goods_list']|json_encode|raw};
$(".buy_goods").html(template("selGoods_tpl",data));
data.goodsList = {$row['repeat_goods_list']|json_encode|raw};
data.select_type = 'repeat_goods';
$(".repeat_goods").html(template("selGoods_tpl",data));


//处理管理奖
function evelLimitRole(){	
	$('#d_role_box').html('');
	$('.selectRole').each(function(i,v){
		if ($(this).prop("checked")) {
			var val = [];
			val.role_name = $(this).data('role_name');
			val.role_id = $(this).val();
			$('#d_role_box').append(template('d_role_tr',val));
		}
	})	
}
//优先加载
if (award_type == 3) {
    var d_userRole = {$UsersRole|json_encode|raw};
    $.each(award_value,function(i,v){
        v.role_name = d_userRole[i].role_name;
        v.role_id = i;
        $('#d_role_box').append(template('d_role_tr',v));
    })
}else if (award_type == 4){
        $.each(award_value,function(i,v){
            v.val = d_level[i];
            v.key = i;
            var roleList = [];
            $('.selectRole').each(function(i,v){
                var val = [];
                val.role_name = $(this).data('role_name');
                val.role_id = $(this).val();
                roleList.push(val);
            })
            v.roleList = roleList;
            $('#d_role_goods_box').append(template('d_role_goods_tr',v));
        })
}else{
    $.each(award_value,function(i,v){
        v.val = d_level[i];
        v.key = i;

        $('#d_level_box').append(template('d_level_tr',v));
    })
}


$('.selectRole').click(function(){
    evelLimitRole();
})


function addLevel(){
    var _award_type = $("input[name='award_type']:checked").val();
    if (_award_type == 4){
        var length = $('.d_role_goods_tr').length;
        if (length >= 9) return _alert("级别不能超过九级！");
        var _level = new Object;
        _level.key = length+1;
        _level.val = d_level[length+1];
        _level.num = [];
        _level.num_type = [];
        var roleList = [];
        $('.selectRole').each(function(i,v){
            var val = [];
            val.role_name = $(this).data('role_name');
            val.role_id = $(this).val();
            roleList.push(val);
        })
        _level.roleList = roleList;
        $('#d_role_goods_box').append(template('d_role_goods_tr',_level));
    }else{
        var length = $('.d_level_tr').length;
        if (length >= 9) return _alert("级别不能超过九级！");
        var _level = new Object;
        _level.key = length+1;
        _level.val = d_level[length+1];
        $('#d_level_box').append(template('d_level_tr',_level));
    }
}
function delLevel(key){
    var _award_type = $("input[name='award_type']:checked").val();
    if (_award_type == 4) {
        var length = $('.d_role_goods_tr').length;
        if (key < length) return _alert("不允许跨级删除！");
        $('.d_role_goods_tr').eq(key - 1).remove();
    }else{
        var length = $('.d_level_tr').length;
        if (key < length) return _alert("不允许跨级删除！");
        $('.d_level_tr').eq(key - 1).remove();
    }

}
</script>

{/block}